package com.androidbook.garbagedemo1;

import androidx.annotation.Nullable;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.androidbook.garbagedemo1.activity.BaseActivity;
import com.androidbook.garbagedemo1.activity.LoginActivity;
import com.androidbook.garbagedemo1.activity.RegisterActivity;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends BaseActivity {
    private Button btnLogin,btnRegister;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnLogin = findViewById(R.id.btn_login);//绑定按钮
        btnRegister = findViewById(R.id.btn_register);

        //跳转页面
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navigateTo(LoginActivity.class);//调用基类的navigateTo进行跳转
            }
        });
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navigateTo(RegisterActivity.class);
            }
        });

            //获取识别的结果
            getRecognitionResult("眼镜");
    }

    private void getRecognitionResult(String goods) {
        //使用Get异步请求
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                //拼接访问地址
                .url("http://api.tianapi.com/txapi/lajifenlei/index?key=783da68c7ea7e10fcd259db651cc550b&word=" + goods)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                if (response.isSuccessful()) {//回调的方法执行在子线程。
                    Log.d("result: ", result);
                }
            }
        });
    }


}